package com.bitBusy.manageyourriches.utilityClasses.adapters;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bitBusy.manageyourriches.R;
import com.bitBusy.manageyourriches.activity.ViewExpensesActivity;
import com.bitBusy.manageyourriches.database.contactClasses.SpendingCategory;
import com.bitBusy.manageyourriches.database.contactClasses.SpendingSubcategory;
import com.bitBusy.manageyourriches.utilityClasses.datastructures.CustomExpandableList;

/**
 * Adapter for expandable list
 * @author SumaHarsha
 *
 */
public class CategoriesExpandableListAdapter extends BaseExpandableListAdapter {

	/**Default children count*/
	private static final int CHILDCOUNT = 1;
	
	/**Context*/
	 private Context myContext;
	 
	 /**list of categories to be added*/
	 private List<SpendingCategory> myCategories;
	  
	 /** map of amounts with categories*/
	 private Map<SpendingCategory, Double> myAmounts;
	 
	 /**from date*/
	 private int myFromDate;
	 
	 /**to date*/
	 private int myToDate;
	 
	 /** list */
	 private ExpandableListView myList;
		 
	 /** subgroup to expand*/
	// private HashMap<SpendingSubcategory, Boolean> myExpandedChildren;
	 
	 /**Constructor
	 * @param the_list */
	 public CategoriesExpandableListAdapter(Context the_context, ExpandableListView the_list, Map<SpendingCategory, Double> the_map, int the_fromDate, int the_toDate) {
		 myContext = the_context;
		 myFromDate = the_fromDate;
		 myToDate = the_toDate;
		 myCategories = (the_map==null)?null:Arrays.asList(the_map.keySet().toArray(new SpendingCategory[the_map.size()]));
		 myAmounts = the_map;
		 myList = the_list;
	 }
	  


	@Override
	 public List<SpendingSubcategory> getChild(int the_groupPosition, int the_childPosition) {
		 return myCategories.get(the_groupPosition).getSubcategories();
	 }
	 
	 @Override
	 public long getChildId(int the_groupPosition, int the_childPosition) {
		 return the_childPosition;
		 }
	 
	 @Override
	 public View getChildView(int the_groupPosition, int the_childPosition, boolean isLastChild, 
	   View the_view, ViewGroup the_parent) {
	   
	  List<SpendingSubcategory> subcategories = (List<SpendingSubcategory>) getChild(the_groupPosition, the_childPosition);
	  CustomExpandableList list = new CustomExpandableList(myContext, the_parent.getWidth(), subcategories.size());
//	  list.setLayoutParams(new RelativeLayout.LayoutParams(the_parent!=null?the_parent.getWidth():LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
      SubcategoriesExpandableListAdapter listAdapter = new SubcategoriesExpandableListAdapter(myContext, this, the_groupPosition, list,subcategories, myFromDate, myToDate);
	  list.setAdapter(listAdapter);
	  return list;
	 }

	/* (non-Javadoc)
	 * @see android.widget.ExpandableListAdapter#getChildrenCount(int)
	 */
	@Override
	public int getChildrenCount(int groupPosition) {
		return CHILDCOUNT;
	}

	/* (non-Javadoc)
	 * @see android.widget.ExpandableListAdapter#getGroup(int)
	 */
	@Override
	public Object getGroup(int the_groupPosition) {
		return myCategories.get(the_groupPosition);
	}

	/* (non-Javadoc)
	 * @see android.widget.ExpandableListAdapter#getGroupCount()
	 */
	@Override
	public int getGroupCount() {
		return myCategories.size();

	}

	/* (non-Javadoc)
	 * @see android.widget.ExpandableListAdapter#getGroupId(int)
	 */
	@Override
	public long getGroupId(int the_groupPosition) {
		return the_groupPosition;

	}

	/* (non-Javadoc)
	 * @see android.widget.ExpandableListAdapter#getGroupView(int, boolean, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getGroupView(int the_groupPosition, boolean isExpanded,
			View the_convertView, ViewGroup the_parent) {
		SpendingCategory category = (SpendingCategory) getGroup(the_groupPosition);
		  if (the_convertView == null) {
		   LayoutInflater inf = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		   the_convertView = inf.inflate(R.layout.category_details_row, null);
		  }
		   
		 ImageView categoryImg = (ImageView) the_convertView.findViewById(R.id.expandableList_Image);
		 categoryImg.setImageResource(category.getPicID());
		 
		 String name = category.getName();
		 TextView categoryName = (TextView) the_convertView.findViewById(R.id.expandableList_DescText);
		 categoryName.setText(name);
		 
		 double amount = myAmounts != null? myAmounts.get(category):0;
		 TextView categoryAmt = (TextView) the_convertView.findViewById(R.id.expandableList_AmtText);
		 categoryAmt.setText(String.valueOf(amount));
		 categoryAmt.setTextColor(myContext.getResources().getColor(R.color.firebrick));
		 
		 the_convertView.setBackgroundColor(myContext.getResources().getColor(R.color.lavendar));
		  return the_convertView;
		 }

	/* (non-Javadoc)
	 * @see android.widget.ExpandableListAdapter#hasStableIds()
	 */
	@Override
	public boolean hasStableIds() {
		return true;

	}

	/* (non-Javadoc)
	 * @see android.widget.ExpandableListAdapter#isChildSelectable(int, int)
	 */
	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
	
		/** collapse group
		 * @param the position
		 */
	public void expandGroup(int the_groupPosition)
	{
		if (myList != null)
		{
			myList.expandGroup(the_groupPosition);
		}
	}
	 
	/** method to update amount for a category
	 * @param group position*/
	public void updateGroup(int the_groupPosition, int the_subgroupPosition)
	{
		SpendingCategory category = myCategories.get(the_groupPosition);
		//myExpandSubgroup = category.getName() + the_subgroupPosition;
		ViewExpensesActivity currentActivity = (ViewExpensesActivity) myContext;
		currentActivity.updateData(category, this);
	}
	
	 
	/** method to update amount in map
	 * @param category
	 */
	public void updateMapAmount(SpendingCategory the_category, double the_amount)
	{
		if (the_category != null)
		{
		//	int groupPosition = myCategories.indexOf(the_category);
			myAmounts.put(the_category, the_amount);
			notifyDataSetChanged();
		}
	}



	
}
